package com.sicnu.cheer.generalmodule.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.regex.Pattern;

/**
 * 日期工具类
 *
 * @author cheer
 */
public class DateUtil {

    /**
     * 判断活动创建日期是否为三天之前
     * @param createDay 活动创建日期
     * @return
     */
    public static boolean isThreeDaysAgo(String createDay) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long daySpan = 0;
        try {
            long time1 = formatter.parse(createDay).getTime();
            long time2 = System.currentTimeMillis();
            long aDay=24*60*60*1000;
            daySpan=(time2-time1)/aDay;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (daySpan>=3){
            return true;
        }else {
            return false;
        }
    }
    /**
     * 根据生日计算年龄
     * @param birthDate
     * @return
     */
    public static int getAge(Date birthDate) {
        if (birthDate == null)
            throw new RuntimeException("出生日期不能为null");
        int age = 0;
        Date now= new Date();
        SimpleDateFormat format_y = new SimpleDateFormat("yyyy");
        SimpleDateFormat format_M = new SimpleDateFormat("MM");

        String birth_year = format_y.format(birthDate);
        String this_year = format_y.format(now);

        String birth_month = format_M.format(birthDate);
        String this_month = format_M.format(now);

        // 初步，估算
        age = Integer.parseInt(this_year) - Integer.parseInt(birth_year);

        // 如果未到出生月份，则age - 1
        if(this_month.compareTo(birth_month) < 0)
            age -=1;
        if (age <0)
            age =0;
        return age;
    }

    public static String formateDate(Date date, String partten) {
        String nowtime = null;
        SimpleDateFormat ch = null;
        try {
            ch = new SimpleDateFormat(partten,
                    Locale.CHINESE);

            nowtime = ch.format(date);
            return nowtime;
        } catch (Exception e) {
            e.printStackTrace();
            ch = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                    Locale.CHINESE);

            nowtime = ch.format(new Date());
            return nowtime;
        }

    }
    /**
     * 获取当前时间
     *
     * @return
     */
    public static long getNowTime() {
        return new Date().getTime();
    }

    /**
     * 字符型日期转为Date类型
     * @param dateStr
     * @param pattern
     * @return
     */
    public static Date convertDate(String dateStr, String pattern) {
        Date d1 = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            d1 = sdf.parse(dateStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return d1;
    }

    /**
     * 获取当前年份
     *
     * @return
     */
    public static int getCurrentYear() {
        Calendar ca = Calendar.getInstance();
        return ca.get(Calendar.YEAR);
    }

}
